@ECHO OFF

set VERSION=%1
set CLICKONCE_VERSION=%2
set APP_MANIFEST_DIR=Microsoft.EnhancedReminders.OutlookAddIn_%CLICKONCE_VERSION%
set FRIENDLY_NAME=Microsoft Enhanced Reminders Outlook Add-in

REM We need to resign the manifests for both the application and the deployment manifests
REM When we enable the right version number we will need to fix this script.

set BUILD_TARGET=%_NTTREE%\Dev\Ocps\Applications\EnhancedReminders\ClientInstaller

REM We are using the dummy key here, not the real MS key. Still need to figure that out and do that after signing.
set CERT_FILE=%_LCSROOT%\dev\ocps\apps\EnhancedReminders\OutlookAddIn\OutlookAddIn_TemporaryKey.pfx

REM It's a stupid syntax, but mage.exe looks recursively through the named directory for DLLs. So we need to have a directory
REM with only the binaries we want. Notice that we take them from %BUILD_TARGET% so that we pick up signed versions if available.

mkdir "%BUILD_TARGET%\RefAssemblies"
copy /y "%BUILD_TARGET%\..\Microsoft.OfficeCommunicationsServer.Applications.Common.dll" "%BUILD_TARGET%\RefAssemblies\"
copy /y "%BUILD_TARGET%\..\Microsoft.EnhancedReminders.Common.dll" "%BUILD_TARGET%\RefAssemblies\"
copy /y "%BUILD_TARGET%\..\Microsoft.EnhancedReminders.OutlookAddIn.dll" "%BUILD_TARGET%\RefAssemblies\"
copy /y "%BUILD_TARGET%\..\CommunicatorAPI.dll" "%BUILD_TARGET%\RefAssemblies\"

REM Replace the friendly name with something more ... friendly
perl -p -i.bak -e "s/friendlyName>Microsoft.EnhancedReminders.OutlookAddIn/friendlyName>%FRIENDLY_NAME%/" "%BUILD_TARGET%\%APP_MANIFEST_DIR%\Microsoft.EnhancedReminders.OutlookAddIn.dll.manifest"
del "%BUILD_TARGET%\%APP_MANIFEST_DIR%\Microsoft.EnhancedReminders.OutlookAddIn.dll.manifest.bak"

REM Resign first the application manifest
mage.exe -u "%BUILD_TARGET%\%APP_MANIFEST_DIR%\Microsoft.EnhancedReminders.OutlookAddIn.dll.manifest" -cf "%CERT_FILE%" -fd "%BUILD_TARGET%\RefAssemblies" -name "%FRIENDLY_NAME%" -version %VERSION%

rmdir /s /q "%BUILD_TARGET%\RefAssemblies"

REM Then resign the deployment manifest
mage.exe -u "%BUILD_TARGET%\Microsoft.EnhancedReminders.OutlookAddIn.vsto" -cf "%CERT_FILE%" -appm "%BUILD_TARGET%\%APP_MANIFEST_DIR%\Microsoft.EnhancedReminders.OutlookAddIn.dll.manifest" -pub "Microsoft" -name "%FRIENDLY_NAME%" -version %VERSION%